package com.hliushi.learn.controller;

import com.hliushi.learn.bean.Reader;
import com.hliushi.learn.dao.ReaderDao;
import com.hliushi.learn.exception.BusinessErrorException;
import com.hliushi.learn.exception.BusinessMsgEnum;
import com.hliushi.learn.response.JsonResult;
import com.hliushi.learn.service.ReaderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName ReaderController
 * @Description TODO
 * @Author hliushi
 * @Date 2021/7/7 14:51
 * @Version V1.0
 **/
@RestController
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private ReaderDao readerDao;

    @Autowired
    private ReaderService readerService;

    /**
     * 获取所有读者信息
     *
     * @return jsonResult
     */
    @ApiOperation("获取所有读者信息")
    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    public JsonResult<List<Reader>> getUserAll() {
        List<Reader> readerList = readerDao.getReaderAll();
        if (readerList != null) {

            return new JsonResult<>(readerList);
        } else {
            return new JsonResult<>("500", "readerList is empty!");
        }
    }

    /**
     * 根据读者的id获取读者详情信息
     *
     * @return jsonResult
     */
    @ApiOperation("根据读者的id获取读者详情信息")
    @GetMapping("/get/{readerId}")
    public JsonResult<Reader> getReaderById(@PathVariable Integer readerId) {

        Reader reader = readerDao.getReaderById(readerId);
        if (reader != null) {
            return new JsonResult<>(reader);
        } else {
            return new JsonResult<>();
        }
    }


    /**
     * 添加读者信息
     *
     * @param reader 添加的读者信息
     * @return jsonResult
     */
    @ApiOperation("添加读者信息")
    @PutMapping("/insert")
    public JsonResult<Void> insertReader(@RequestBody Reader reader) {
        if (reader != null) {
            readerService.insertReader(reader);
        } else {
            throw new BusinessErrorException(BusinessMsgEnum.PARAMETER_EXCEPTION);
        }

        return new JsonResult<>();
    }

    /**
     * 添加读者信息, 测试异常并没有被 "捕获" 到
     *
     * @param reader 添加的读者信息
     * @return jsonResult
     */
    @ApiOperation("添加读者信息, , 测试异常并没有被 \"捕获\" 到")
    @PutMapping("/insert2")
    public JsonResult<Void> insertReader2(@RequestBody Reader reader) throws SQLException {
        if (reader != null) {
            readerService.insertReader2(reader);
        } else {
            throw new BusinessErrorException(BusinessMsgEnum.PARAMETER_EXCEPTION);
        }

        return new JsonResult<>();
    }


    /**
     * 添加读者信息, 测试异常被 "吃" 掉
     *
     * @param reader 添加的读者信息
     * @return jsonResult
     */
    @ApiOperation("添加读者信息, , 测试异常被 \"吃\" 掉")
    @PutMapping("/insert3")
    public JsonResult<Void> insertReader3(@RequestBody Reader reader) {
        if (reader != null) {
            readerService.insertReader3(reader);
        } else {
            throw new BusinessErrorException(BusinessMsgEnum.PARAMETER_EXCEPTION);
        }

        return new JsonResult<>();
    }


}
